//
//  ViewController.m
//  RunLoopDemo
//
//  Created by zhangjikuan on 2020/4/16.
//  Copyright © 2020 hsgd. All rights reserved.
//

#import "ViewController.h"
#import <WebKit/WebKit.h>
#import "Person.h"
#import "ViewControllerTableViewCell.h"
#import "AQTask.h"
#import "AQBasicFactory.h"
#import "EcodeTask.h"
#import "NodeTask.h"
#import "AQGCDTask.h"
#import "AQIphoneManager.h"
#import "NSString+Add.h"
#import "AQLagMonitor.h"
#import "RespondChainViewController.h"
#import "EventChainTestTask.h"
#import "ForwarTask.h"
#import "RunLoopDemo-Swift.h"
#import "AnimateViewController.h"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
{
    CFRunLoopObserverRef runloopObserver;
}

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, copy) NSArray *sectionTitles;

@end

@implementation ViewController
- (NSString *)reversalString:(NSString *)originString{
    NSString *resultStr = @"";
    for (NSInteger i = originString.length -1; i >= 0; i--) {
      NSString *indexStr = [originString substringWithRange:NSMakeRange(i, 1)];
      resultStr = [resultStr stringByAppendingString:indexStr];
    }
  return resultStr;
}

- (NSString *)removeDuplicates:(NSString *)S {
    NSMutableArray *inStk = [NSMutableArray array];
    for (int i = 0; i < S.length; i ++) {
        NSString *subStr = [S substringWithRange:NSMakeRange(i, 1)];
        [inStk addObject:subStr];
    }
    
    NSMutableArray *outStk = [NSMutableArray array];
    for (NSString *ch in inStk) {
        if (outStk.count != 0 && [outStk.lastObject isEqualToString:ch]) {
            [outStk removeObject:outStk.lastObject];
        } else {
            [outStk addObject:ch];
        }
    }
    
    NSMutableString *mStr = [NSMutableString string];
    for (NSString *ch in outStk) {
        [mStr appendString:ch];
    }
    return (NSString *)mStr;
}

- (NSString *)add:(NSString *)str1 other:(NSString *)str2 {

    if (str1 == nil || str1.length == 0) {
        return str2;
    }
    
    if (str2 == nil || str2.length == 0) {
        return str1;
    }
    
        
    // 反转字符串
    // A 1123 -> 3211
    // B 2456 -> 6542
    // 一位一位相加
    // 9
    // 7
    // 5
    // 2
    // 字符串拼接到新的字符串
    
    str1 = [str1 revers];
    str2 = [str2 revers];

    // carr sum/10;
    
    // carr > 1;
    
    // 进行翻操作
    
    /// 数组长度
    ///
    /**
     1、字符串转数组
     2、字符串翻转
     3、ASCII码加减处理
     4、数字相加进位处理
     */
    
    NSString *shortString = str1.length <= str2.length ? str1:str2;
    NSString *longString = str1.length < str2.length ? str2:str1;
    

    unsigned char css1[shortString.length];
    unsigned char css2[longString.length];
    memcpy(css1, [shortString cStringUsingEncoding:NSUTF8StringEncoding], shortString.length);
    memcpy(css2, [longString cStringUsingEncoding:NSUTF8StringEncoding], longString.length);

    unsigned long minLength = MIN(str1.length, str2.length);
    unsigned long maxLength = MAX(str1.length, str2.length);

    NSMutableString *muStr = [NSMutableString string];
    int carry = 0; // 标志进位 0
    int a=0,b=0,c=0;
    
    for (int i = 0; i < maxLength; i++) { // 遍历大的字符串数组
        // short
        if (i < minLength) {
            a = css1[i] - '0';
            b = css2[i] - '0';
            c = (a + b + carry)%10;
            carry = (a+b+carry)/10;
            NSLog(@"carry %d", carry);
        }
        
        if (i >= minLength && i< maxLength) {
            b = css2[i] - '0';
            NSLog(@"carryMaxB %c", css2[i]);
            c = (b + carry)%10;
            carry = (b+carry)/10;
            NSLog(@"carryMax %d", carry);

        }
        
        if (i==maxLength-1 && carry == 1) {
            c = 1;
        }

        [muStr appendString:[NSString stringWithFormat:@"%d", c]];
        
    }
    
    NSLog(@"mStr %@", [muStr revers]);
    
    return [muStr revers];
}
static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
    
}

- (void)dealloc {
    [[AQLagMonitor shareInstance] endMonitor];
    
}

- (int)calculate:(NSString *)S {

    // scan s
    NSMutableArray *inStk = [NSMutableArray array];
    for (int i = 0; i < S.length; i ++) {
        NSString *subStr = [S substringWithRange:NSMakeRange(i, 1)];
        [inStk addObject:subStr];
        NSInteger lastValue;
        NSString *lastOp;
        NSString *nextOp;
        
        
        if ([subStr isEqualToString:@"+"])
        {
            
        } else if ([subStr isEqualToString:@"-"])
        {
            
        } else if ([subStr isEqualToString:@"("])
        {
            
        } else if([subStr isEqualToString:@")"])
        {
            
        } else {
            
        }
    }
    
    NSMutableArray *outStk = [NSMutableArray array];
    for (NSString *ch in inStk) {
        if (outStk.count != 0 && [outStk.lastObject isEqualToString:ch]) {
            [outStk removeObject:outStk.lastObject];
        } else {
            [outStk addObject:ch];
        }
    }
    
    NSLog(@"%@", outStk);
    
    
    
    
    return 0;
}
// 遍历遇到则...
//

- (void)viewDidLoad {
    NSString *s = @"1+1+1+(3-2)-(2-8)";

    [self calculate:s];
    
    NSString *S = @"abbaca";
    
    S = [self removeDuplicates:S];
    
    NSLog(@"S:%@", S);
    
    
    [self add:@"110101" other:@"1000000000"];
    [super viewDidLoad];

    [[AQLagMonitor shareInstance] beginMonitor];
    
    NSLog(@"AA %@",[self intToRoman:3]);
    NSLog(@"AA %@",[self intToRoman:4]);
    NSLog(@"AA %@",[self intToRoman:9]);
    NSLog(@"AA %@",[self intToRoman:58]);
    NSLog(@"AA %@",[self intToRoman:1994]);
    
    
    
//    WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.bounds
//                                            configuration:[WKWebViewConfiguration new]];
    
//    [self.view addSubview:webView];
//
//    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.jianshu.com/p/856cefbbf4b0"]]];
    
    [self.view addSubview:self.tableView];
    
    iPhoneType phoneType = [AQIphoneManager iphone];
    
    NSLog(@"phoneType %lu", (unsigned long)phoneType);
    AQIphoneManager *manager = [[AQIphoneManager alloc] init];
    [manager onNext];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.section) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                    AQTask *task = [AQBasicFactory taskWith:AQTaskTypeCopyAndStrong];
                    [task execute];
                }
                    break;
                case 1: {
                    AQTask *task = [AQBasicFactory taskWith:AQJSOCTypeTask];
                    [task executeBy:self];
                }
                    break;
               case 2:
                {
                    AQTask *task = [AQBasicFactory taskWith:AQTaskTypeBlock];
                    [task execute];
                }
                    break;
                case 3:
                {
                    EcodeTask *task = [[EcodeTask alloc] init];
                    [task execute];
                }
                    break;
                case 4:
                {
                    NodeTask *task = [[NodeTask alloc] init];
                    [task execute];
                }
                    break;
                case 5:
                {
                    AQGCDTask *task = [[AQGCDTask alloc] init];
                    [task execute];
                }
                    break;
                case 6:
                {
                    EventChainTestTask *task = [[EventChainTestTask alloc] init];
                    [task executeBy:self];
                }
                    break;
                case 7:
                {
                    ForwarTask *task = [[ForwarTask alloc] init];
                    [task execute];
                }
                    break;
                case 8:
                {
                    AnimateViewController *controller = [[AnimateViewController alloc] init];
                    [self presentViewController:controller animated:YES completion:nil];
                }
                    break;
                default:
                    break;
            }
            
            break;
        case 1:
            switch (indexPath.row) {
                case 0:{
                   
                    TestVC *vc = [[TestVC alloc] init];
                    [self presentViewController:vc animated:YES completion:nil];
                    
                }break;
                    
                default:
                    break;
            }
            
        default:
            break;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 10)];
    label.text = @"section";
    
    return label;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ViewControllerTableViewCell.description forIndexPath:indexPath];
    cell.textLabel.text = self.dataSource[indexPath.section][indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataSource[section] count];
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:NSClassFromString(@"ViewControllerTableViewCell") forCellReuseIdentifier:ViewControllerTableViewCell.description];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
        title.textAlignment = NSTextAlignmentCenter;
        
        title.text = @"iOS基础回顾";
        _tableView.tableHeaderView = title;
        
    }
    return _tableView;
}

- (NSArray *)sectionTitles {
    if (!_sectionTitles) {
        _sectionTitles = @[@"基础",@"进阶",@"高级"];
    }
    return _sectionTitles;
}

- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        NSArray *list1 = @[@"属性修饰",@"JSOC通信",@"ios Block 本质",@"Ecode", @"单链表反转", @"GCD",@"EventChainTestTask",@"ForwarTask 消息转发原理实例研究",@"animation"];
        
        NSArray *list2 = @[@"none",@"none",@"none"];
        NSArray *list3 = @[@"none",@"none"];
        [_dataSource addObjectsFromArray:@[list1,list2,list3]];
    }
    return _dataSource;
}


// 整数转为罗马数字
//
- (NSString *)intToRoman:(int)num {
    
    NSMutableString *mString = [NSMutableString string];
    // num 的值大小
    for (int i = 0; i<num/1000; i++) {
        [mString appendString:@"M"];
    }
    
    num = num%1000;
    
    // num = 900 和 num = 400
    int bNum = num/100;
    if (bNum == 9) {
        [mString appendString:@"CM"];
    } else if (bNum > 4 && bNum < 9) {
        [mString appendString:@"D"];
        for (int i = 0; i< bNum - 5; i ++) {
            [mString appendString:@"C"];
        }
    } else if (bNum == 4) {
        [mString appendString:@"CD"];
    } else {
        for (int i = 0; i<bNum; i++) {
            [mString appendString:@"C"];
        }
    }
    
    num = num%100;
    bNum = num/10;
    if (bNum == 9) {
        [mString appendString:@"XC"];
    } else if (bNum > 4 && bNum < 9) {
        [mString appendString:@"L"];
        for (int i = 0; i< bNum - 5; i ++) {
            [mString appendString:@"X"];
        }
    } else if (bNum == 4) {
        [mString appendString:@"XL"];
    } else {
        for (int i = 0; i<bNum; i++) {
            [mString appendString:@"X"];
        }
    }
    
    num = num%10;
    bNum = num;
    
    if (bNum == 9) {
        [mString appendString:@"IX"];
    } else if (bNum > 4 && bNum < 9) {
        [mString appendString:@"V"];
        for (int i = 0; i< bNum - 5; i ++) {
            [mString appendString:@"I"];
        }
    } else if (bNum == 4) {
        [mString appendString:@"IV"];
    } else {
        for (int i = 0; i<bNum; i++) {
            [mString appendString:@"I"];
        }
    }
    
    
    return (NSString *)mString;
}

- (int)romanToInt:(NSString *)s {
    if (s.length==0) {
        return 0;
    }
    
    return 0;
}
@end
